package user

import (
	"fmt"
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/user"
	"gq_volunteer_platform/service/message_queue"

	"github.com/gin-gonic/gin"
)

func ExistList(c *gin.Context) {

	var req struct {
		Page         int    `binding:"required,gte=1"`
		Limit        int    `binding:"required,gte=1"`
		Name         string `binding:"max=255"`
		RealName     string `binding:"max=255"`
		MobileNumber string `binding:"max=255"`
		Status       string `binding:"oneof='' 待审核 已驳回 已审核"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var offset = (req.Page - 1) * req.Limit

	// 查询
	var count int64
	var list []struct {
		ID             uint `gorm:"primarykey;->"`
		OrganizationId uint
		Name           string
		RealName       string
		ProfilePhoto   string
		MobileNumber   string

		Status    string
		CreatedAt string
	}

	var tx = bean.MysqlDb.Model(&user.UserExist{}).
		Select("`user_exist`.`id`,`user_exist`.`status`,`user_exist`.`created_at`,`user`.`name`,`user`.`profile_photo`,`user`.`real_name`,`user`.`mobile_number`,`user`.`organization_id`").
		Joins("LEFT JOIN `user` on `user`.`id` = `user_exist`.`user_id`")

	if req.Status != "" {
		tx.Where("`user_exist`.`status`=?", req.Status)
	}

	if req.Name != "" {
		tx.Where("`user`.`name` like ?", "%"+req.Name+"%")
	}

	if req.RealName != "" {
		tx.Where("`user`.`real_name` like ?", "%"+req.RealName+"%")
	}

	if req.MobileNumber != "" {
		tx.Where("`user`.`mobile_number` like ?", "%"+req.MobileNumber+"%")
	}

	// 查询总数
	if err := tx.Count(&count).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查询列表
	if err := tx.Limit(req.Limit).Offset(offset).Order("`id` DESC").
		Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"count": count,
		"list":  list,
	})
}

// 软删除
func ExistCheck(c *gin.Context) {

	var req struct {
		ID     uint   `binding:"max=10000000"`
		Status string `binding:"oneof='已审核 已驳回"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	var exi user.UserExist
	if err := bean.MysqlDb.Where("`id`=?", req.ID).First(&exi).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("表单不存在id:%d", req.ID))
		return
	}

	// 状态相同跳过
	// if exi.Status == req.Status {
	// 	c.String(200, "ok")
	// 	return
	// }

	var exi2 user.UserExist
	exi2.ID = exi.ID
	exi2.Status = req.Status

	if err := bean.MysqlDb.Where("`id`=?", req.ID).Updates(&exi2).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("表单不存在id:%d", req.ID))
		return
	}

	// 已审核 软删除用户
	if req.Status == "已审核" {
		var one user.User

		// if err := bean.MysqlDb.Model(&user.User{}).Where("`id`=?", exi.UserId).First(&one).Error; err != nil {
		// 	bean.Log.Warning(err)
		// 	c.String(400, fmt.Sprintf("用户不存在id:%d", req.ID))
		// 	return
		// }

		if err := bean.MysqlDb.Where("`id`=?", exi.UserId).Delete(&one).Error; err != nil {
			bean.Log.Warning(err)
			c.String(500, err.Error())
			return
		}
	}

	c.String(200, "ok")

	// 发送通知 jldtx
	var user_list struct {
		ID        uint
		Openid    string
		CreatedAt string
	}
	if err := bean.MysqlDb.Unscoped().Model(&user.User{}).Where("`id`=?", exi.UserId).First(&user_list).Error; err != nil {
		bean.Log.Error(err)
		return
	}

	var Openid = user_list.Openid
	var page = "pages/user/main/index"
	var applyProject = "志愿者注销申请"
	var applyTime = exi.CreatedAt
	var applyResult = ""
	if req.Status == "已审核" {
		applyResult = "您的申请已通过"
	} else if req.Status == "已驳回" {
		applyResult = "您的申请已驳回"
	}
	if err := message_queue.MessageQueueAddT1(Openid, page, applyProject, applyTime, applyResult); err != nil {
		// 错误记录
		bean.Log.Error(err)
	}
}
